package com.ziq.zcamp.place

import android.graphics.Color
import android.graphics.drawable.GradientDrawable
import android.view.View
import android.view.ViewGroup
import androidx.core.view.isVisible
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.donkingliang.groupedadapter.holder.BaseViewHolder
import com.facebook.imagepipeline.request.ImageRequest
import com.ziq.base.extension.click
import com.ziq.base.extension.dp
import com.ziq.base.view.drawable.gradientDrawableBuild
import com.ziq.zcamp.R
import com.ziq.zcamp.bean.place.DetailGroup
import com.ziq.zcamp.bean.place.IntroduceItem
import com.ziq.zcamp.bean.place.PlaceDetail
import com.ziq.zcamp.bean.post.ImageInfo
import com.ziq.zcamp.constant.FollowRelation
import com.ziq.zcamp.databinding.ItemPlaceDetailIntroduceBinding
import com.ziq.zcamp.databinding.ItemPlaceDetailIntroduceFooterBinding
import com.ziq.zcamp.databinding.ItemPlaceDetailIntroduceHeaderBinding
import com.ziq.zcamp.dialog.AlertDialog
import com.ziq.zcamp.life.CampLifeActivity
import com.ziq.zcamp.login.ActionAfterLogin
import com.ziq.zcamp.login.LoginActivity
import com.ziq.zcamp.manager.GlobalContextManager
import com.ziq.zcamp.manager.UserManager
import com.ziq.zcamp.mine.PersonalHomePageActivity
import com.ziq.zcamp.mine.PicturePreviewActivity
import com.ziq.zcamp.util.ImageUtil
import com.ziq.zcamp.util.TimeUtil

class PlaceIntroduceDetailGroup(val activity: PlaceDetailActivity, val adapter: PlaceDetailAdapter) : DetailGroup() {

    override fun getHeaderViewType(groupPosition: Int): Int {
        return PlaceDetailAdapter.TYPE_HEADER_INTRODUCE
    }

    override fun getChildViewType(groupPosition: Int, childPosition: Int): Int {
        return PlaceDetailAdapter.TYPE_CHILD_INTRODUCE
    }

    override fun getFooterViewType(groupPosition: Int): Int {
        return PlaceDetailAdapter.TYPE_FOOTER_INTRODUCE
    }

    var placeDetail: PlaceDetail? = null

    //详情
    fun setData(placeDetail: PlaceDetail?) {
        this.placeDetail = placeDetail
    }

    override fun hasHeader(groupPosition: Int): Boolean {
        return true
    }

    override fun hasFooter(groupPosition: Int): Boolean {
        return true
    }

    override fun getChildrenCount(): Int {
        return 1
    }

    override fun onBindHeaderViewHolder( holder: BaseViewHolder?, groupPosition: Int) {
        holder?.let {
            val binding = ItemPlaceDetailIntroduceHeaderBinding.bind(holder.itemView)
            val lp = binding.container.layoutParams as StaggeredGridLayoutManager.LayoutParams
            if(groupPosition == 0){
                lp.topMargin = 0
            } else {
                lp.topMargin = 8.dp
            }
            binding.container.layoutParams = lp
        }
    }

    override fun onBindChildViewHolder(
        holder: BaseViewHolder?,
        groupPosition: Int,
        childPosition: Int
    ) {

        val layoutParams = holder?.itemView?.layoutParams
        if (layoutParams != null && layoutParams is StaggeredGridLayoutManager.LayoutParams) {
            layoutParams.isFullSpan = true
        }
        holder?.let {
            val binding = ItemPlaceDetailIntroduceBinding.bind(holder.itemView)
            if(adapter.countGroupItem(groupPosition) == holder.adapterPosition + 1){
                binding.root.setBackgroundResource(R.drawable.bg_white_0_0_5_5)
            }else {
                binding.root.setBackgroundResource(R.drawable.bg_white_0_0_0_0)
            }
            binding.webview.resizeForContent = true
            binding.webview.loadData(placeDetail?.camp?.content?:"", "text/html", "UTF-8")
        }
    }

    override fun onBindFooterViewHolder(holder: BaseViewHolder?, groupPosition: Int) {
        holder?.let {
            val binding = ItemPlaceDetailIntroduceFooterBinding.bind(holder.itemView)

            val isFieldCamp = placeDetail?.camp?.camp_type == 2
            binding.clFieldCampSafeTips.isVisible = isFieldCamp
            if(isFieldCamp){
                binding.btnCheckTips.click {
//                    WebActivity.openWebView(GlobalContextManager.appContext!!, H5Url.WILD_CAMP_PRIVACY.url!!, "")
                }
                binding.clFieldCampSafeTips.background = gradientDrawableBuild {
                    solidColor = Color.parseColor("#FAFBF8")
                    cornerRadius = 4.dp.toFloat()
                }
                binding.vTipsMask.background = gradientDrawableBuild {
                    gradientAngle = GradientDrawable.Orientation.TOP_BOTTOM
                    gradientColors = intArrayOf(Color.parseColor("#00F9FAF8") , Color.parseColor("#F9FAF8"))
                }
            }



            val uid = placeDetail?.user?.uid
            binding.name.text = placeDetail?.user?.nick_name ?:""
            binding.tvCampLifeBadge.setCampNum(placeDetail?.user?.camping_count)
            binding.tvCampLifeBadge.click {
                CampLifeActivity.open(activity, placeDetail?.user?.uid?:0)
            }
            val publishAt = GlobalContextManager.getString(R.string.publish_at)
            binding.time.text = "$publishAt ${placeDetail?.camp?.publish_time}"
            ImageUtil.updateImageFromNetwork(binding.avator, placeDetail?.user?.header_url)

            binding.root.click {
                uid?.let { it1 -> PersonalHomePageActivity.open(activity, it1) }
            }

            if(UserManager.instance.isMine(placeDetail?.user?.uid?:0)){
                binding.btnFollow.visibility = View.GONE
            }

            UserManager.instance.userFollowRelation.value?.let {
                if(it.containsKey(uid)){
                    when {
                        it[uid] == FollowRelation.mutual.value -> {
                            binding.btnFollow.isSelected = true
                            binding.btnFollow.setText(R.string.mutual_follow)
                            binding.btnFollow.setTextColor(Color.parseColor("#C5BFBB"))
                        }
                        it[uid] == FollowRelation.following.value -> {
                            binding.btnFollow.isSelected = true
                            binding.btnFollow.setText(R.string.followed)
                            binding.btnFollow.setTextColor(Color.parseColor("#C5BFBB"))
                        }
                        else -> {
                            binding.btnFollow.isSelected = false
                            binding.btnFollow.setText(R.string.follow)
                            binding.btnFollow.setTextColor(Color.parseColor("#AF8B6C"))
                        }
                    }
                }
            }

            binding.btnFollow.click {
                LoginActivity.checkLogin ( object : ActionAfterLogin {
                    override fun action() {
                        UserManager.instance.apply {
                            if(isLogin.value == true){
                                if(binding.btnFollow.isSelected){
                                    onUnFollow()
                                } else {
                                    followUser(placeDetail?.user?.uid?:0)
                                }
                            }
                        }
                    }
                })
            }
        }
    }

    private fun onUnFollow(){
        AlertDialog(activity)
            .setTitle {
                it.append(GlobalContextManager.getString(R.string.are_you_sure_unfollow))
            }
            .setCancelButton(text = {it.append(GlobalContextManager.getString(R.string.cancel))}, action = {it.dismiss()})
            .setConfirmButton(text = {it.append(GlobalContextManager.getString(R.string.confirm))}, action = {
                it.dismiss()
                UserManager.instance.unfollowUser(placeDetail?.user?.uid?:0)
            })
            .show()
    }

}